﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;

namespace GPSTrackingClientsWebsiteManager.Espanol.Pages
{
    public partial class EmployeeRoutes : Telerik.Web.UI.RadAjaxPage
    {
        private void GetImeiByUsername(string Username)
        {
            EmployeeRoutesGrid.DataSource = GPSTrackingServerDBCustom.Data.DataRepository.ImeiProvider.GetDataTableImeiByUsername(Username);
            EmployeeRoutesGrid.Columns[4].Visible = false;
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

      
        /*protected void ImeiGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                foreach (GridDataItem item in ImeiGrid.Items)
                {
                    RadComboBox RadComboBox1 = item.FindControl("RadComboBox1") as RadComboBox;

                    if (!RadComboBox1.SelectedValue.Equals(item.Cells[6].Text))
                    {
                        GPSTrackingServerDB.Entities.GroupsByImei groupsByImeiEntity = new GPSTrackingServerDB.Entities.GroupsByImei();
                        groupsByImeiEntity.Imei = item.Cells[2].Text;
                        groupsByImeiEntity.GroupId = Convert.ToInt32(RadComboBox1.SelectedValue);
                        groupsByImeiEntity.OriginalGroupId = Convert.ToInt32(item.Cells[6].Text);
                        groupsByImeiEntity.OriginalImei = item.Cells[2].Text;

                        GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Update(groupsByImeiEntity);
                    }
                }

                GetImeiByUsername(Request.QueryString["Username"]);
                ImeiGrid.DataBind();

                RadNotification1.Show();
            }
            catch (Exception ex)
            {

                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','GPS error ...');", true);
            } 
        }*/

        protected void EmployeeRoutesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            EmployeeRoutesGrid.DataSource = GPSTrackingServerDBCustom.Data.DataRepository.ImeiProvider.GetDataTableImeiByUsername(Request.QueryString["Username"]);
            EmployeeRoutesGrid.Columns[4].Visible = false;
        }

        protected void EmployeeRoutesGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            try
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem gridDataItem = e.Item as GridDataItem;
                 
                    RadComboBox RadComboBox0 = gridDataItem.FindControl("RadComboBox0") as RadComboBox;
                    RadComboBox RadComboBox1 = gridDataItem.FindControl("RadComboBox1") as RadComboBox;
                    RadComboBox RadComboBox2 = gridDataItem.FindControl("RadComboBox2") as RadComboBox;
                 
                    // Create a new DataTable.
                    System.Data.DataTable table = new DataTable("ParentTable");
                    // Declare variables for DataColumn and DataRow objects.
                    DataColumn column;
                    DataRow row;

                    // Create new DataColumn, set DataType, 
                    // ColumnName and add to DataTable.    
                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.Int32");
                    column.ColumnName = "id";
                    column.ReadOnly = true;
                    column.Unique = true;
                    // Add the Column to the DataColumnCollection.
                    table.Columns.Add(column);

                    // Create second column.
                    column = new DataColumn();
                    column.DataType = System.Type.GetType("System.String");
                    column.ColumnName = "route";
                    column.AutoIncrement = false;
                    column.Caption = "route";
                    column.ReadOnly = false;
                    column.Unique = false;
                    // Add the column to the table.
                    table.Columns.Add(column);

                    // Make the ID column the primary key column.
                    DataColumn[] PrimaryKeyColumns = new DataColumn[1];
                    PrimaryKeyColumns[0] = table.Columns["id"];
                    table.PrimaryKey = PrimaryKeyColumns;

                    // Instantiate the DataSet variable.
                    DataSet dataSet = new DataSet();
                    // Add the new DataTable to the DataSet.
                    dataSet.Tables.Add(table);

                    // Create three new DataRow objects and add 
                    // them to the DataTable
                    row = table.NewRow();
                    row["id"] = 0;
                    row["route"] = "Ruta San Joaquin";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["id"] = 1;
                    row["route"] = "Ruta San Pedro";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["id"] = 2;
                    row["route"] = "Ruta Coronado";
                    table.Rows.Add(row);

                    RadComboBox1.DataSource = dataSet;
                    RadComboBox1.DataTextField = "route";
                    RadComboBox1.DataValueField = "id";
                    RadComboBox1.DataBind();


                    RadComboBox1.SelectedValue = e.Item.Cells[6].Text;

                    table.Clear();

                    // Create three new DataRow objects and add 
                    // them to the DataTable
                    row = table.NewRow();
                    row["id"] = 0;
                    row["route"] = "8:00 am - 12 pm";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["id"] = 1;
                    row["route"] = "1:00 pm - 5 pm";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["id"] = 2;
                    row["route"] = "6:00 pm - 10 pm";
                    table.Rows.Add(row);
                    
                    RadComboBox2.DataSource = dataSet;
                    RadComboBox2.DataTextField = "route";
                    RadComboBox2.DataValueField = "id";
                    RadComboBox2.DataBind();


                    RadComboBox2.SelectedValue = e.Item.Cells[6].Text;

                    table.Clear();

                    // Create three new DataRow objects and add 
                    // them to the DataTable
                    row = table.NewRow();
                    row["id"] = 0;
                    row["route"] = "Juan Perez";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["id"] = 1;
                    row["route"] = "Alberto Ramirez";
                    table.Rows.Add(row);

                    row = table.NewRow();
                    row["id"] = 2;
                    row["route"] = "Jose Francisco Mora";
                    table.Rows.Add(row);

                    RadComboBox0.DataSource = dataSet;
                    RadComboBox0.DataTextField = "route";
                    RadComboBox0.DataValueField = "id";
                    RadComboBox0.DataBind();
                }
            }
            catch (Exception ex)
            {

                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Grupos error ...');", true);
            }  
        }
    }
}